// JavaScript Document

// Routines
var h=0;

function init()
{
  t=setTimeout('checkMessages()',1);
}
//
function checkMessages()
{
  h=h+1;
	document.getElementById('messageCounter').innerHTML=h;
}

//


// Ajax calls


function ajaxMove(data)
{
	ajax('move',data);
}

//
function ajaxToggle(data)
{
	ajax('toggle',data);
}

//
function ajaxDelete(data)
{
  if (confirm("Are you sure you want to delete")) {
   ajax('delete',data);
  }	
}

//
function ajaxList(data)
{
	ajax('list',data);
}

//
function ajaxEdit(data)
{
	ajax('edit',data);
}

//
function ajaxCall(data)
{
	ajax('call',data);
}

//
function ajaxInsert(data)
{
	ajax('insert',data);
}

//
function ajaxForm(file,formName)
{
	var data = buildPostData(formName);
	ajax(file,data);
}

//
function buildPostData(theFormName) {
    theForm = document.forms[theFormName];
    var qs = '';
    for (var e=0;e<theForm.elements.length;e++) {
        if (theForm.elements[e].name!='') {
         if (theForm.elements[e].type == 'checkbox') {
            if (theForm.elements[e][i].checked == true) {
                var name = theForm.elements[e].name;
                qs+=(qs=='')?'':'&'
                qs+= name+'='+encodeURIComponent(theForm.elements[e].value);
            }
        }else if (theForm.elements[e].type=='radio') {

              if(theForm.elements[e].checked == true){
                var name = theForm.elements[e].name;
                qs+=(qs=='')?'':'&'
                qs+= name+'='+encodeURIComponent(theForm.elements[e].value);
              }
        }else if (theForm.elements[e].type=='select-one') {
         
          for(j =0; j< theForm.elements[e].options.length; j++) 
          {         
                if(theForm.elements[e].options[j].selected == true) 
                {               
                 var name = theForm.elements[e].name;
                 var v=theForm.elements[e].options[j].value;
                 qs+=(qs=='')?'':'&';
                 qs+=name+'='+v;
                }
           }
        }else
        {
            var name = theForm.elements[e].name;
            qs+=(qs=='')?'':'&'
            qs+= name+'='+encodeURIComponent(theForm.elements[e].value);       
        }
      }//encodeURIComponent
      //escape(encodeURI(theForm.elements[e].value));
    }
    //qs+="\n" ?? do I need this;
    return qs
}


//
var http_request = false;


   function ajax( file, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open("POST", 'ajax/'+file+'.php', true); 
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }
//
   function alertContents() {
     document.getElementById('messages').innerHTML = '<img src=\"images/wait.gif\">';
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            eval(result);           
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
